<template>
  <div class="wrapper" ref="wrapper">
    <div class="content">
      <slot></slot>
    </div>
  </div>
</template>

<script>
  import BScroll from 'better-scroll'

  export default {
    name: "Scroll",
    props: {
      probeType: {
        type: Number,
        default: 0
      },
      pullUpLoad: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        scroll: null
      }
    },
    mounted() {
      // 1. 创建BScroll对象
      this.scroll = new BScroll(this.$refs.wrapper, {
        click: true,
        scrollY: true,
        observeImage:true,
        observeDOM: true,
        probeType: this.probeType,
        pullUpLoad: true
      })
      // 2. 监听滚动的位置
      this.scroll.on('scroll', (position)=>{
        this.$emit('scroll', position)
      })
      // 3. 上拉加载更多
      this.scroll.on('pullingUp', ()=>{
        this.$emit('pullingUp')
      })
    },
    methods: {
      // 封装重新计算高度的方法
      refresh() {
        this.scroll && this.scroll.refresh()
      },
      // 返回当前的y值
      getCurrentY() {
        return this.scroll.y ? this.scroll.y : 0
      }
    },
  }
</script>

<style scoped>

</style>